<template>
   <!-- 商品分类导航 -->
   <div class="type-nav">
            <div class="container">
                <!-- 事件委托 当鼠标进入时，search模块显示分类列表 -->
               <div @mouseleave="leaveIndex"  @mouseenter="enterShow">
                <h2 class="all">全部商品分类</h2>
<transition name="sort">
    <!-- 鼠标离开时，search模块隐藏分类列表 -->
 <!-- 谁显示隐藏谁有transition效果 -->
<div class="sort" v-show="show" @mouseleave="leaveShow">
    <div class="all-sort-list2" @click="go_search">
        <!-- 三级联动 -->
        <div class="item bo" v-for="(c1,index) in categroyList" :key=c1.categoryId :class="{cur:index===currentIndex}">
            <h3 @mouseenter="changeIndex(index)">
                <a :data-categoryName="c1.categoryName" :data-category1Id="c1.categoryId" >{{c1.categoryName}}</a>
            </h3>
               <!-- 二级分类js控制显示与隐藏 -->
            <div class="item-list clearfix" :style="{display:currentIndex === index ? 'block' : 'none'}">
                <div class="subitem" v-for="(c2,index) in c1.categoryChild" :key="c2.categoryId">
                    <dl class="fore">
                        <dt>
                            <a :data-categoryName="c2.categoryName" :data-category2Id="c2.categoryId">{{c2.categoryName}}</a>
                        </dt>
                        <dd>
                            <em  v-for="(c3,index) in c2.categoryChild" :key="c3.categoryId">
                                <a :data-categoryName="c1.categoryName" :data-category3Id="c1.categoryId">{{c3.categoryName}}</a>
                            </em>
                        </dd>
                    </dl>
                </div>
            </div>
        </div>
       
    </div>
</div>
</transition>
               </div>

                <nav class="nav">
                    <a href="###">服装城</a>
                    <a href="###">美妆馆</a>
                    <a href="###">尚品汇超市</a>
                    <a href="###">全球购</a>
                    <a href="###">闪购</a>
                    <a href="###">团购</a>
                    <a href="###">有趣</a>
                    <a href="###">秒杀</a>
                </nav>
                
            </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import throttle from 'lodash/throttle'
export default {
name:'TypeNav',
data(){
    return{
        // 鼠标当前分类的索引
        currentIndex:-1,
        // 是否显示分类列表（home模块/search模块）
        show:true
    }
},
mounted(){
    //切换组件时，home会进行组件销毁，search会组件创建，当挂载完毕时不显示分类列表
    if(this.$route.name != 'home') this.show=false
},
computed:{
    // 对象写法
 ...mapState({
    categroyList:state=>state.home.getCategoryList
 })

},
methods:{
    // 获取鼠标当前分类的索引
    changeIndex:throttle(function(index){     
        // 不能使用箭头函数，会改变上文
        // 为了避免用户操作过快导致出现浏览器卡顿，使用节流函数
        this.currentIndex = index
    },50),
    // 鼠标离开失去索引
    leaveIndex(){
        this.currentIndex = -1
    },
    /*
    三级连动的路由跳转
    1>（声明式路由导航）将每一个a标签转换为router-link标签 缺点：会生成多个组件，而每一个组件的生成很耗内存，导致出现不流畅现象
    2>使用编程式路由导航 缺点：给多个a标签绑定事件，会导致代码冗余，不易维护
    3>采用事件委托+编程式路由导航

    解决问题:
    1>得到点击的元素
    2>得到该元素的categoryName值和categoryId值
    */
   go_search(event){
    let ele = event.target
    let {categoryname,category1id,category2id,category3id} = ele.dataset
    
    if(categoryname){
     let location = {name:'search'}
     let query = {categoryName:categoryname}
     if(category1id){
        query.category1Id=category1id
       }else if(category2id){
        query.category2Id=category2id
       }else{
        query.category3Id=category3id
      }

   //如果有params参数，则添加到location中   
     if(this.$route.params){
        location.params = this.$route.params
        location.query=query
        this.$router.push(location)
     }
    }
   },
   // search模块鼠标进入显示分类列表
   enterShow(){
    this.show = true
   },
   // search模块鼠标离开隐藏分类列表
   leaveShow(){
    if(this.$route.name != 'home'){
        this.show = false
    }
   }
}
}
</script>

<style lang="less" scoped>
.type-nav {
        border-bottom: 2px solid #e1251b;

        .container {
            width: 1200px;
            margin: 0 auto;
            display: flex;
            position: relative;

            .all {
                width: 210px;
                height: 45px;
                background-color: #e1251b;
                line-height: 45px;
                text-align: center;
                color: #fff;
                font-size: 14px;
                font-weight: bold;
            }

            .nav {
                a {
                    height: 45px;
                    margin: 0 22px;
                    line-height: 45px;
                    font-size: 16px;
                    color: #333;
                }
            }

            .sort {
                position: absolute;
                left: 0;
                top: 45px;
                width: 210px;
                height: 461px;
                position: absolute;
                background: #fafafa;
                z-index: 999;

                .a{
                    cursor: pointer !important;
                }

                .all-sort-list2 {
                    .item {
                        h3 {
                            line-height: 30px;
                            font-size: 14px;
                            font-weight: 400;
                            overflow: hidden;
                            padding: 0 20px;
                            margin: 0;

                            a {
                                color: #333;
                            }
                        }

                        .item-list {
                            display: none;
                            position: absolute;
                            width: 734px;
                            min-height: 460px;
                            background: #f7f7f7;
                            left: 210px;
                            border: 1px solid #ddd;
                            top: 0;
                            z-index: 9999 !important;

                            .subitem {
                                float: left;
                                width: 650px;
                                padding: 0 4px 0 8px;

                                dl {
                                    border-top: 1px solid #eee;
                                    padding: 6px 0;
                                    overflow: hidden;
                                    zoom: 1;

                                    &.fore {
                                        border-top: 0;
                                    }

                                    dt {
                                        float: left;
                                        width: 54px;
                                        line-height: 22px;
                                        text-align: right;
                                        padding: 3px 6px 0 0;
                                        font-weight: 700;
                                    }

                                    dd {
                                        float: left;
                                        width: 415px;
                                        padding: 3px 0 0;
                                        overflow: hidden;

                                        em {
                                            float: left;
                                            height: 14px;
                                            line-height: 14px;
                                            padding: 0 8px;
                                            margin-top: 5px;
                                            border-left: 1px solid #ccc;
                                        }
                                    }
                                }
                            }
                        }

                     
                    }
                }
            }

            // 开始（进入）
            .sort-enter{
                height: 0;
            }
            // 结束（进入）
            .sort-enter-to{
                height:461px;
            }
            //行为(进入)
            .sort-enter-active{
                transition: all 0.3s ease;
            }

            //开始(离开)
            .sort-leave{
                height: 461px;
            }   
            //结束(离开)
            .sort-leave-to{
                height: 0;
            }
            //行为(离开)
            .sort-leave-active{
                transition: all 0.3s ease;
            }
        }
        .cur {
            background-color:skyblue;
        }
    }
</style>